import http from '@/server/request.js';
import * as Utils from '@/utils/index.js';
import * as Auth from '@/utils/auth';
import { PRO_ART_BASEURL, DEV_ART_BASEURL } from '@/constant/index.js';

const baseUrl = process.env.NODE_ENV === 'development' ? DEV_ART_BASEURL : PRO_ART_BASEURL;

// 注：api是APP-H5，apiPass是微信登录

/**
 * 分页查询奖品任务
 * @param {Number} "id"	活动ID
 * @param {String} "lotteryName"	活动名称
 * @param {String} "lotteryDescription"	活动说明
 * @param {Number} "lotteryType"	活动抽奖状态每日0 活动时间1
 */
export const getJob = async (params) => {
  const data = await http.request({
    baseUrl,
    url: `/apiPass/lottery/pageJob`,
    method: 'GET',
    // header,
    data: params,
  });

  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取活动失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取活动失败'));
  }
  return data;
};

/**
 * 奖品选项
 * @param {Number} "lotteryJobId"	活动ID
 */
export const getOption = async (lotteryJobId) => {
  const data = await http.request({
    baseUrl,
    url: `/apiPass/lottery/option/${lotteryJobId}`,
    method: 'GET',
  });

  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取奖品失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取奖品失败'));
  }
  return data;
};

/**
 * 自己的中奖查询
 * @param {Number} "lotteryJobId"	活动ID
 */
export const isLottery = async (lotteryJobId) => {
  let header = { auth: true };
  const data = await http.request({
    baseUrl,
    url: `/api/lotteryTemplate/isLottery/${lotteryJobId}`,
    method: 'GET',
    header,
  });
  if (data.data.code === '401') {
    Utils.appInteractionH5.tologin();
    return;
  }
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '中奖查询失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '中奖查询失败'));
  }
  return data;
};

/**
 * 自己中奖查询列表（新）
 * @param {Number} "lotteryJobId"	活动ID
 */
export const isLotteryList = async (lotteryJobId) => {
  let header = { auth: true };
  const data = await http.request({
    baseUrl,
    url: `/api/lotteryTemplate/isLottery/list/${lotteryJobId}`,
    method: 'GET',
    header,
  });
  if (data.data.code === '401') {
    Utils.appInteractionH5.tologin();
    return;
  }
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '中奖查询失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '中奖查询失败'));
  }
  return data;
};

/**
 * 是否参与抽奖
 * @param {Number} "commodityId"	商品选项ID
 */
export const isLotteryByJobId = async (lotteryJobId) => {
  let header = { auth: true };
  const data = await http.request({
    baseUrl,
    url: `/api/lotteryTemplate/isLotteryByJobId/${lotteryJobId}`,
    method: 'GET',
    header,
  });
  if (data.data.code === '401') {
    Utils.appInteractionH5.tologin();
    return;
  }
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '中奖查询失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '中奖查询失败'));
  }
  return data;
};

/**
 * 中奖名单获取
 * @param {Number} "lotteryJobId"	活动ID
 */
export const isWinning = async (lotteryJobId) => {
  let header = { auth: true };
  const data = await http.request({
    baseUrl,
    url: `/api/lotteryTemplate/isWinning/${lotteryJobId}`,
    method: 'GET',
    header,
  });
  if (data.data.code === '401') {
    Utils.appInteractionH5.tologin();
    return;
  }

  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '中奖名单获取失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '中奖名单获取失败'));
  }
  return data;
};

/**
 * 抽奖
 * @param {Number} "lotteryJobId"	抽奖对应的活动ID
 */
export const lottery = async (lotteryJobId) => {
  let header = { auth: true };
  const data = await http.request({
    baseUrl,
    url: `/api/lotteryTemplate/getLottery/${lotteryJobId}`,
    method: 'GET',
    header,
  });
  if (data.data.code === '401') {
    Utils.appInteractionH5.tologin();
    return;
  }

  if (!data.data.success && data.data.code === '4004') return data;
  if (!data.data.success && data.data.message.includes('没有抽奖资格')) return data;

  if (!data.data.success) {
    if (data.data.code === '2000' || data.data.code === '2001') {
      uni.showToast({
        title: '您已没有抽奖机会',
        duration: 2000,
        icon: 'none',
      });
      return Promise.reject(new Error(data.data.message || '中奖名单获取失败'));
    }
    uni.showToast({
      title: data.data.message || '抽奖失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '抽奖失败'));
  }
  return data;
};

/**
 * 中奖信息填写
 * @param {Number} "lotteryJobId"	抽奖对应的活动ID
 * @param {String} "address"	邮寄地址
 * @param {String} "phone"	手机号码
 * @param {String} "userName"	姓名
 */
export const lotteryInfo = async (params) => {
  let header = { auth: true };
  const data = await http.request({
    baseUrl,
    url: `/api/lotteryTemplate/LotteryInfo`,
    method: 'POST',
    header,
    data: params,
  });
  if (data.data.code === '401') {
    Utils.appInteractionH5.tologin();
    return;
  }
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '提交失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '提交失败'));
  }
  return data;
};

/**
 * 分页查询抽奖模板（获取页面配置信息）
 * @param {Number} "lotteryJobId"	活动ID
 */
export const pageLotteryModule = async (params) => {
  let header = { auth: true };
  const data = await http.request({
    baseUrl,
    url: `/apiPass/lottery/pageLotteryModule`,
    method: 'GET',
    // header,
    data: params,
  });

  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '抽奖模板获取失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '抽奖模板获取失败'));
  }
  return data;
};

/**
 * 获取用户剩余可抽奖次数
 * @param {Number} "lotteryJobId"	活动ID
 */
export const getLotteryCount = async (lotteryJobId) => {
  let header = { auth: true };
  const data = await http.request({
    baseUrl,
    url: `/api/lotteryTemplate/getLotteryCount/${lotteryJobId}`,
    method: 'GET',
    header,
  });
  if (data.data.code === '401') {
    Utils.appInteractionH5.tologin();
    return;
  }

  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取用户剩余可抽奖次数失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取用户剩余可抽奖次数失败'));
  }
  return data;
};

/**
 * 获取用户剩余可抽奖次数（针对指定名单抽奖）
 * @param {Number} "jobId"	活动ID
 */
export const getRosterLimit = async (lotteryJobId) => {
  let header = { auth: true };
  const data = await http.request({
    baseUrl,
    url: `/api/lotteryTemplate/getRosterLimit`,
    method: 'GET',
    header,
    data: {
      jobId: lotteryJobId,
    },
  });
  if (data.data.code === '401') {
    Utils.appInteractionH5.tologin();
    return;
  }

  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取用户剩余可抽奖次数失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取用户剩余可抽奖次数失败'));
  }
  return data;
};

/**
 * 添加资格到可抽奖指定名单
 * @param {Number} "jobId"	活动ID
 */
export const setRoster = async (lotteryJobId) => {
  let header = { auth: true, encryption: true };
  const data = await http.request({
    baseUrl,
    url: `/api/lotteryTemplate/setRoster`,
    method: 'POST',
    header,
    data: {
      jobId: lotteryJobId,
    },
  });
  if (data.data.code === '401') {
    Utils.appInteractionH5.tologin();
    return;
  }

  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取抽奖资格失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取抽奖资格失败'));
  }
  return data;
};

/**
 * 获取用户剩余可抽奖次数（针对指定名单抽奖）
 * @param {Number} "jobId"	活动ID
 */
export const setRosterLimit = async (lotteryJobId) => {
  let header = { auth: true, encryption: true };
  const data = await http.request({
    baseUrl,
    url: `/api/lotteryTemplate/setRosterLimit`,
    method: 'POST',
    header,
    data: {
      jobId: lotteryJobId,
    },
  });
  if (data.data.code === '401') {
    Utils.appInteractionH5.tologin();
    return;
  }

  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '添加抽奖次数失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '添加抽奖次数失败'));
  }
  return data;
};
